import Vue from 'vue';
import Axios from 'axios';
import Qs from 'qs';
import VModal from 'vue-js-modal';
import paging from '../components/pageing/paging.js';
import '../basic/basic.css';
import './list.css';
import Clip from 'clipboard/dist/clipboard.min.js';
Vue.use(VModal,{dialog:true});

new Vue({
    el:'#louie-list',
    data:{
        //--url--
        url:$('#louie-list').attr('data-url'),
        activity_id:0,  //操作的活动id
        //--nav--
        nav:[
            {name:'未开始',status_phases:1,status:1,active:true},
            {name:'进行中',status_phases:2,status:1,active:false},
            {name:'已结束',status_phases:3,status:1,active:false},
            {name:'待审核',status_phases:0,status:2,active:false}
        ],
        nav_index:0,
        //--列表--
        list:[],
        returnNum:10,  //请求数据数量
        pageNum:1,  //当前page页面
        total:0,
        //--活动后台核销管理--
        account:'',  //账号
        password:'',   //密码
        confirm_password:'',  //确认密码
        //其他
        clip:null,  //粘贴板操作对象
        //搜索
        search:false,  //是否搜索列表
        search_keyword:'',
        search_type:'id'
    },
    components:{
      'paging':paging
    },
    created:function(){
        this.getTotal();
        this.li();
    },
    computed:{
      admin_url:function(){
          return $('#louie-list').attr('data-admin-url')+'&activity_id='+this.activity_id;
      }
    },
    methods:{
        //点击导航
        changeNav:function(index){
            if(this.nav_index != index){
                this.search = false;
                this.nav[this.nav_index].active = false;
                this.nav_index = index;
                this.nav[index].active = true;
                this.pageNum = 1;
                this.getTotal();
                this.li();
            }
        },
        //获取数据总数
        getTotal:function(){
            var data = {
                pages:'activity',
                doing:'total',
                status_phases:this.nav[this.nav_index].status_phases,
                status:this.nav[this.nav_index].status
            }
            if(this.search){
                data.search_type = this.search_type;
                data.search_keyword = this.search_keyword;
            }
            Axios.post(this.url,Qs.stringify(data))
                .then((res)=>{
                    this.total = parseInt(res.data.data);
                })
                .catch(error=>console.log(error));
        },
        //加载列表
        li:function(){
            var data = {
                pages:'activity',
                doing:'li',
                limit:[(this.pageNum-1)*this.returnNum,this.returnNum],
                status_phases:this.nav[this.nav_index].status_phases,
                status:this.nav[this.nav_index].status
            }
            if(this.search){
                data.search_type = this.search_type;
                data.search_keyword = this.search_keyword;
            }
            Axios.post(this.url,Qs.stringify(data))
                .then((res)=>{
                    if(res.data.status == 'success'){
                        this.list = res.data.data;
                    }else{
                        this.list = [];
                    }
                })
                .catch(error=>console.log(error));
        },
        //连接修改详情页面
        goDetail:function(event){
            window.location.href = event.target.getAttribute('data-href')+'#doing=edit#activity_id='+event.target.getAttribute('data-id');
        },
        //删除活动询问
        clickDel:function(activity_id){
            var self = this;
            this.confirm('确定要删除该活动吗',function(is_true){
                if(is_true){
                    self.del(activity_id);
                }
            });
        },
        //删除活动
        del:function(activity_id){
            var self = this;
            Axios.post(this.url,Qs.stringify({
                pages:'activity',
                doing:'delete',
                activity_id:activity_id
            }))
                .then((res)=>{
                    if(res.data.status == 'error'){
                        self.alert('删除失败');
                    }else{
                        self.getTotal();
                        self.li();
                    }
                })
                .catch(error=>console.log(error));
        },
        //显示后台管理账号和地址
        clickAdmin:function(activity_id){
            this.activity_id = activity_id;
            this.$modal.show('admin-modal');
        },
        //admin管理区域显示前执行
        beforeAdminModal:function(){
          Axios.post(this.url,Qs.stringify({
              pages:'activity',
              doing:'findAdmin',
              activity_id:this.activity_id
          }))
              .then((res)=>{
                  var data = res.data;
                  this.password = '';
                  this.confirm_password = '';
                  if(data.status == 'success'){
                    this.account = data.data.account;
                  }else{
                     this.account = '';
                  }
              })
              .catch(error=>console.log(error));
        },
        //关闭后台管理账号和地址
        closeAdminModal:function(){
            this.$modal.hide('admin-modal');
        },
        //前往管理后台，自动免登录
        goAdmin:function(){
            Axios.post(this.url,Qs.stringify({
                pages:'admin',
                doing:'autoLogin',
                activity_id:this.activity_id
            }))
                .then((res)=>{
                    if(res.data.status == 'success'){
                        window.open(res.data.data,'_blank');
                    }
                })
                .catch(error=>console.log(error));
        },
        //复制活动后台管理地址
        copyAdmin:function(){
            this.clip = new Clip('.copy-admin-url');
            this.clip.on('success',()=>{
                this.alert('复制成功');
            });
        },
        //确认修改后台管理账号密码
        submitAdmin:function(){
            if(!this.submitAdminCheck()) return;
            Axios.post(this.url,Qs.stringify({
                pages:'activity',
                doing:'setAdmin',
                activity_id:this.activity_id,
                account:this.account,
                password:this.password
            }))
                .then((res)=>{
                    if(res.data.status == 'success'){
                        this.alert('设置成功');
                    }else{
                        this.alert(res.data.err_msg);
                    }
                })
                .catch(error=>console.log(error));
        },
        //确认修改后台管理账号密码验证
        submitAdminCheck:function(){
            if(this.account == ''){
                this.alert('后台管理账号不能为空');
                return false;
            }
            if(this.account.length > 20){
                this.alert('后台管理账号长度不能大于20');
                return false;
            }
            if(this.password.length < 6){
                this.alert('密码长度不能小于6');
                return false;
            }
            if(this.password != this.confirm_password){
                this.alert('两次密码不一致');
                return false;
            }
            return true;
        },
        //点击分页
        changePage:function(page){
            this.pageNum = page;
            this.li();
        },
        //确认搜索
        searchSubmit:function(){
          if(this.search_keyword == ''){
                this.alert('请输入搜索内容');
                return false;
          }
          this.search = true;
          this.pageNum = 1;
          this.getTotal();
          this.li();
        },
        //审核活动
        checkActivity(activity_id){
            Axios.post(this.url,Qs.stringify({
                pages:'activity',
                doing:'checkActivity',
                activity_id
            }))
                .then((res)=>{
                    if(res.data.status == 'success'){
                        this.getTotal();
                        this.li();
                    }else{
                        this.alert(res.data.err_msg);
                    }
                })
                .catch(error=>console.log(error));
        },
        //alert
        alert:function(text){
            this.$modal.show('dialog',{
                text:text,
                buttons:[
                    {title:'关闭'}
                ]
            });
        },
        //confirm
        confirm:function(text,call){
            var self = this;
            this.$modal.show('dialog',{
                text:text,
                buttons:[
                    {
                        title: '取消',
                        handler:function(){
                            self.$modal.hide('dialog');
                            call(false);
                        }
                    },
                    {
                        title: '确认',
                        default: true,
                        handler:function(){
                            self.$modal.hide('dialog');
                            call(true);
                        }
                    }
                ]
            });
        }
    }
});